package main

import (
	"context"
	"fmt"
	"log/slog"
	"os"

	"github.com/jackc/pgx/v5"
	"github.com/tawesoft/golib/v2/dialog"
)

func main() {
	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	logger.Info("Program start")
	// urlExample := "postgres://username:password@localhost:5432/database_name"
	conn, err := pgx.Connect(context.Background(), "postgres://postgres@localhost:5432/E2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(context.Background())
	_, err = conn.Exec(context.Background(), "CREATE USER kksp WITH PASSWORD 'kksp01!';")
	if err != nil {
		dialog.Alert("Unable to create user:  %v", err)
		fmt.Println(err)
	}
	_, err = conn.Exec(context.Background(), `GRANT ALL PRIVILEGES ON DATABASE "E2" to kksp;`)
	if err != nil {
		dialog.Alert("Unable to grant privileges to user:  %v", err)
		fmt.Println(err)
	}
	dialog.Alert("Успешно добавлен пользователь")
}
